<?php
declare(strict_types=1);

namespace Service\MemberService\Volidate;

use App\Model\BbcModel\Member;
use App\Model\BbcModel\Mnuser;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\ShoppingGuide;
use Service\PayService\CashPayService;

class MemberVolidate extends BaseService
{
    CONST DEFATUL_FREE_DAY = 365;

    public function checkRegisterMemberParam(int $cid, int $mn_sid, array $body = [],int $flag=1)
    {
        //检查会员参数
        if($flag){
            $mnuser = Mnuser::instance()->info(['id' => $mn_sid]);
            if ($mnuser['member_share'] == 2) {
                $where = [
                    'account' => $body['account'],
                    'mn_sid' => $mn_sid,
                    'cid' => $cid,
                ];
            } else {
                $mnuser_ids = Mnuser::instance()->where(['pid' => $cid, 'member_share' => 1])->pluck('id');
                $where = [
                    ['account', '=', $body['account']],
                    ['mn_sid', 'in', $mnuser_ids],
                    ['cid', '=', $cid],
                ];
            }
            $member = Member::instance()->info($where);
            if (!empty($member)) {
                return Normal::returnCode(Normal::FAIL, '账号已经存在');
            }
        }else{
            if(!isset($body['mid'])){
                return Normal::returnCode(Normal::FAIL, '会员信息必传');
            }
        }

        if (!isset($body['is_pay'])) {
            return Normal::returnCode(Normal::FAIL, '是否支付必传');
        }
        if (!isset($body['vip'])) {
            return Normal::returnCode(Normal::FAIL, '是否会员必传');
        }

        if ($body['is_pay'] && $body['vip']) {
            if (empty($body['pay'])) {
                return Normal::returnCode(Normal::FAIL, '支付信息不能为空');
            }
            $pay = @json_decode($body['pay'], TRUE);
            if( empty($pay) ) {
                return Normal::returnCode(Normal::FAIL, '支付方式不能为空');
            }
            if (count($pay) > 1) {
                return Normal::returnCode(Normal::FAIL, '会员费不能混合支付');
            }
            $payerCheck = CashPayService::instance()->getPayChannel($pay, $body, CashPayService::PRODUCT_MEMBER_ORDER_PAY);
            if ($payerCheck['code'] != Normal::SUC) {
                return $payerCheck;
            }
            $body          = $payerCheck['data'];
        }
        return Normal::returnCode(Normal::SUC, '成功',$body);
    }

    /**
     * 账户创建验证
     * @param    array $param
     * @access   public
     * @return   array
     */
    public function validateCreate(array $param = []):array
    {
        if (empty($param['account'])) {
            return Normal::returnCode(Normal::FAIL,'账号不能为空');
        }
        if (empty($param['cid'])) {
            return Normal::returnCode(Normal::FAIL,'渠道id不能为空');
        }

        $where = [
            'account' => $param['account'],
            'mn_sid' => $param['mn_sid']??0,
            'cid' => $param['cid'],
        ];
        $member= Member::instance()->info($where);

        if(!empty($param['guide_code'])){
            $info = ShoppingGuide::instance()->info(['code'=>trim($param['guide_code']),'mn_sid'=>$param['mn_sid'],'cid'=>$param['cid']]);
            if(empty($info)){
                return Normal::returnCode(Normal::FAIL,'导购员不存在');
            }
            if($info['status']==0){
                return Normal::returnCode(Normal::FAIL,'导购员已禁用');
            }
        }
        if (!empty($member)) {
            return Normal::returnCode(Normal::FAIL,'账号已经存在');
        }

        if (empty($param['password'])) {
            return Normal::returnCode(Normal::FAIL,'密码不能为空');
        }
        return Normal::returnCode(Normal::SUC,'验证通过');
    }
}